package com.wuwangfu.func;

import com.wuwangfu.entity.EventBean;
import org.apache.flink.api.common.functions.AggregateFunction;

import java.util.HashMap;

/**
 * @Description：增量聚合函数
 * @Author：jcshen
 * @Date：2023-07-06
 *
 */
public class UrlsAggregateFunc01 implements AggregateFunction<EventBean, HashMap<String,Long>,HashMap<String,Long>> {

    /**
     * 初始化一个hashmap，key就是url，value就是访问次数
     *
     * @return
     */
    @Override
    public HashMap<String, Long> createAccumulator() {
        return new HashMap<>();
    }

    /**
     * 把所有url进行一次聚合，如果添加了就访问次数+1，如果没添加过就设置初始访问次数为1
     *
     * @param event
     * @param map
     * @return
     */
    @Override
    public HashMap<String, Long> add(EventBean event, HashMap<String, Long> map) {
        if (map.containsKey(event.getUrl())){
            map.put(event.getUrl(),map.get(event.getUrl()) + 1);
        }else {
            map.put(event.getUrl(),1L);
        }
        return map;
    }

    @Override
    public HashMap<String, Long> getResult(HashMap<String, Long> map) {
        return map;
    }

    @Override
    public HashMap<String, Long> merge(HashMap<String, Long> map, HashMap<String, Long> acc) {
        return null;
    }
}
